package com.mm.learn.juc;/**
 * @author wdx
 * @create 2021-08-06 15:35
 */

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Semaphore;

/**
 * @author wdx
 * @date 2021年08月06日 15:35
 * 集合并发安全问题
 */
public class CollectionTest {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 4));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
        list.add("sda");

        /**
         * 1.异常 java.util.ConcurrentModificationException
         * 2.如何解决
         *  2.1 new Vector()
         *  2.2 Collections.synchronizedList(new ArrayList<>())
         *  2.3 new CopyOnWriteArrayList<>()
         * 优化建议
         *
         */


    }
}
